package net.hn.hnms.biz.emgcy.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.hn.hnms.biz.emgcy.domain.MaterialPo;
import net.hn.hnms.biz.emgcy.domain.dto.MaterialDto;
import net.hn.hnms.biz.emgcy.domain.vo.MaterialVo;
import net.hn.hnms.biz.emgcy.listener.MaterialImportListener;
import net.hn.hnms.biz.emgcy.service.MaterialSrvice;
import net.hn.hnms.biz.emgcy.template.MaterialTemplate;
import net.hn.hnms.biz.emgcy.util.ExcelUtils;
import net.hn.hnms.sys.common.core.domain.R;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.core.utils.StringUtils;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import net.hn.hnms.sys.common.satoken.config.AutoSetMineCode;
import net.hn.hnms.sys.system.api.RemoteDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: baijiajun
 * @Date: 2022/9/13
 */
@Api(value = "应急物资模块", tags = {"应急物资"})
@RestController
@RequestMapping("material")
public class MaterialController {
    @Autowired
    private MaterialSrvice materialSrvice;
    @Autowired
    private RemoteDictService remoteDictService;

    @ApiOperation("应急物资列表")
    @GetMapping("pageList")
    @AutoSetMineCode
    public R<ResponsePage<MaterialVo>> pageList(PageQuery pageQuery, MaterialDto materialDto) {
        ResponsePage<MaterialVo> list = materialSrvice.pageList(pageQuery, materialDto);
        return R.ok(list);
    }

    @ApiOperation("应急物资添加")
    @PostMapping("/add")
    public R add(@RequestBody MaterialPo materialPo) {
        if (materialPo.getCode() != null && materialPo.getCode().length > 0) {
            Object[] code = materialPo.getCode();
            String str = "";
            for (Object c : code) {
                str += c + ",";
            }
            String s = str.substring(0, str.length() - 1);
            materialPo.setType(s);
        }
        if (StringUtils.isEmpty(materialPo.getTobuyAt())) {
            materialPo.setTobuyAt(null);
        }
        if (StringUtils.isEmpty(materialPo.getProductAt())) {
            materialPo.setProductAt(null);
        }
        if (StringUtils.isEmpty(materialPo.getValidityAt())) {
            materialPo.setValidityAt(null);
        }
        Boolean flag = materialSrvice.add(materialPo);
        if (flag) {
            return R.ok("新增成功");
        }
        return R.fail("新增失败");
    }

    @ApiOperation("应急物资修改")
    @PutMapping("/update")
    public R update(@RequestBody MaterialPo materialPo) {
        if (materialPo.getCode() != null && materialPo.getCode().length > 0) {
            Object[] code = materialPo.getCode();
            String str = "";
            for (Object c : code) {
                str += c + ",";
            }
            String s = str.substring(0, str.length() - 1);
            materialPo.setType(s);
        }
        Boolean flag = materialSrvice.update(materialPo);
        if (flag) {
            return R.ok("修改成功");
        }
        return R.fail("修改失败");
    }

    @ApiOperation("应急物资删除")
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable Long id) {
        Boolean flag = materialSrvice.delete(id);
        if (flag) {
            return R.ok("删除成功");
        }
        return R.fail("删除失败");
    }


    @ApiOperation(value = "导出数据", position = 1)
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws IOException {
        materialSrvice.export(response);
    }

    /**
     * 导出模板
     */
    @GetMapping(value = "exportTemplate")
    public void exportTemplate(HttpServletResponse response) throws IOException {
        List<MaterialTemplate> list = new ArrayList<>();
        ExcelUtils.exportExcel(response, "应急物资导出模板", MaterialTemplate.class, list);
    }

    /**
     * 导入
     */
    @PostMapping(value = "import")
    public void testImport(MultipartFile file) {
        MaterialImportListener materialImportListener = new MaterialImportListener(materialSrvice, remoteDictService);
        ExcelUtils.importExcel(file, MaterialTemplate.class, materialImportListener);
    }
}
